import { _decorator, Component, Node, tween, v3, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Money')
export class Money extends Component {

    @property({ type: Node })
    targetNode: Node = null;

    @property({ type: Vec3 })
    Dir: Vec3;

    @property
    Speed: number = 0;

    @property
    Dis: number = 0;

    dir: Vec3;

    protected onLoad(): void {
    }

    protected start(): void {
        this.dir = this.normalize(this.Dir);
        this.move();
    }

    normalize(vec: Vec3): Vec3 {
        let length = Math.sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z);
        return v3(vec.x / length, vec.y / length, vec.z / length);
    }

    move() {
        tween(this.node)
            .by(this.Dis / this.Speed, { position: v3(this.dir.x * this.Dis, this.dir.y * this.Dis, 0) })
            .delay(0.2)
            .to(0.5, { worldPosition: this.targetNode.getWorldPosition() })
            .start();
    }

}


